Imports DAL
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Object
Imports System.Configuration
Imports Microsoft.VisualBasic.Strings
Imports MyClasses

Public Class frmBackup
    Dim sFileName As String
    Dim frmPrg As New frmProgressBar
    Private Sub frmBackup_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Database Backup")
        e.Cancel = True
    End Sub

    Private Sub frmBackup_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Database Backup")
        End If
    End Sub

    Private Sub frmBackup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call BCommon.SetFormStandards(Me, False)
        txtBackupDir.Text = My.Application.Info.DirectoryPath.Substring(0, 2) + "\Temp\" + Format(DateTime.Now, "ddMMyyhhmmss") + "\"
    End Sub

    Private Sub cmdOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpenFile.Click

        Try
            txtFileName.Enabled = True
            'sFileName = BCommon.AskSaveAsFile()
            sFileName = BCommon.AskDirectory()

            If sFileName <> "" Then
                'Set backup file path.
                txtFileName.Text = sFileName
                'Export to selected file
            End If
        Catch ex As Exception

        End Try
    End Sub
    Sub Export_Database(ByVal FileName As String)
        Try
            ''CHECK IF BACKUP DIRECTORY EXISTS OR NOT. IF IT EXISTS, THEN DELETE IT.
            ''NOTE THAT THIS BACKUP DIRECTORY IS A TEMPORARY DIRECTORY WHICH WAS INITIALIZED IN DURING FORM LOAD.
            If My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
                My.Computer.FileSystem.DeleteDirectory(txtBackupDir.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
            End If
            ''CREATE TEMPORARY DIRECTORY.
            If Not My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
                My.Computer.FileSystem.CreateDirectory(txtBackupDir.Text)
            End If
            'INITIALIZE A DATATABLE. THIS DATATABLE WILL HOLD ALL DATABASE OBJECTS AND THEIR SCRIPTS.

            frmPrg.SetProgress(4)
            'FOLLOWING PROCEDURE WILL GENERATE SCRIPTS FOR SELECTED TABLES USING SQL-DMO. THIS PROCEDURE ALSO BACK UP DATA USING BCP FUNCTIONS IN SQL-DMO LIBRARY.
            'SCRIPT FILE AND DATA FILES WILL BE SAVED IN TEMPORARY FOLDER.

            'BBackup.GenerateFiles(DTScripts, chkData.Checked, "table", chkStructure.Checked, chkMaster.Checked, chkTrans.Checked, FileName)
            Dim Result As Boolean

            'Result = BBackup.GenerateFiles(txtBackupDir.Text, chkData.Checked, "table", True, chkMaster.Checked, chkTrans.Checked, ChkFull.Checked, chkSystem.Checked, FileName)
            Result = BBackup.GenerateFiles(txtBackupDir.Text, True, "table", True, False, False, ChkFull.Checked, chkSystem.Checked, FileName)

            frmPrg.SetProgress(6)
            My.Computer.FileSystem.DeleteDirectory(txtBackupDir.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
            frmPrg.SetProgress(8)
            If Result Then
                frmPrg.HideProgressbar()
                BCommon.ShowMsgError(10388)
                Me.Close()
                Me.Dispose(True)
            Else
                BCommon.ShowMsgError(10389)
            End If
            frmPrg.HideProgressbar()
            'Since script for all objects and data are now saved to temporary directory, zip this temporary folder to a file with extension *.SQLBACKUP.
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnBackUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackUp.Click
        Try
            If txtFileName.Text = "" Then
                BCommon.ShowMsgError(10430)
                Exit Sub
            End If
            If txtFlNm.Text = "" Then
                BCommon.ShowMsgError(10431)
                Exit Sub
            End If

            btnBackUp.Enabled = False

            frmPrg.TopMost = True
            frmPrg.MaximumValue = 10
            frmPrg.Show()
            frmPrg.SetProgress(1)
            frmPrg.SetProgress(2)
            Me.Refresh()
            If txtFileName.Text.EndsWith("\") = False Then
                frmPrg.SetProgress(3)
                Export_Database(txtFileName.Text + "\" + txtFlNm.Text + ".SQLBackup")
            Else
                frmPrg.SetProgress(3)
                Export_Database(txtFileName.Text + txtFlNm.Text + ".SQLBackup")
            End If
            btnBackUp.Enabled = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnBackUp_Click")
        End Try
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
        Me.Dispose(True)
    End Sub

    Private Sub txtFlNm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFlNm.KeyDown
        If BCommon.ValidateText(e.KeyCode, "ALFNUM", False) = False Then
            e.SuppressKeyPress = True
        End If
        If (e.KeyCode = 191) Or (e.KeyCode = 190) Or (e.KeyCode = 32) Then
            e.SuppressKeyPress = True
        End If
    End Sub
End Class